<!--
	Copyright (C) 2010 Ethan Gruber
	Numishare: http://code.google.com/p/numishare/
	Apache License 2.0
	
	Heavily modified from upload form developed by Orbeon	
-->
<html xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xforms="http://www.w3.org/2002/xforms"
	xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns="http://www.w3.org/1999/xhtml" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:fr="http://orbeon.org/oxf/xml/form-runner">
	<head>
		<title>Numishare: Manage CSV Imports</title>
		<link type="text/css" href="http://localhost:8080/cocoon/numishare/xforms.css"
			rel="stylesheet"/>
		<xforms:model>
			<xforms:instance id="imports">
				<imports xmlns=""/>
			</xforms:instance>

			<xforms:instance id="timestamp">
				<timestamp xmlns=""/>
			</xforms:instance>

			<xforms:instance id="current-id">
				<id xmlns=""/>
			</xforms:instance>

			<!-- delete from Solr -->
			<xforms:instance id="deleteId">
				<delete xmlns="">
					<id/>
				</delete>
			</xforms:instance>

			<!-- Instance for Solr commit-->
			<xforms:instance id="sendCommit">
				<commit/>
			</xforms:instance>
			
			<!-- Solr optimize -->
			<xforms:instance id="optimizeIndex">
				<optimize/>
			</xforms:instance>
			
			<!-- load URLs from site_info.xml in eXist into form for Solr and CSS file -->
			<xforms:instance id="site-info">
				<site_info xmlns="">
					<url/>
					<solr_url/>					
				</site_info>
			</xforms:instance>
			
			<!-- load site info -->
			<xforms:submission id="load-site-info" serialization="none" method="get"
				action="/exist/rest/db/numishare/site_info.xml" xxforms:username="admin"
				xxforms:password="admin" replace="instance" instance="site-info"/>

			<xforms:submission id="delete-submission"
				action="/exist/rest/numishare/ead/{instance('current-id')}.xml" method="delete"
				replace="none">
				<xforms:send ev:event="xforms-submit-done" submission="delete-from-solr"/>
			</xforms:submission>

			<!-- delete from Solr -->
			<xforms:submission id="delete-from-solr" action="{instance('site-info')/solr_url}update"
				ref="instance('deleteId')" instance="deleteId" replace="none" method="post"/>

			<!-- send commit -->
			<xforms:submission id="submitCommit" action="{instance('site-info')/solr_url}update"
				ref="instance('sendCommit')" instance="sendCommit" replace="none" method="post">				
				<xforms:message ev:event="xforms-submit-error" level="modal">Data Failed to POST to
				Solr!</xforms:message>
				<xforms:send submission="optimize" ev:event="xforms-submit-done"/>
			</xforms:submission>

			<xforms:submission id="load-imports" serialization="none" method="get"
				action="/exist/rest/db/numishare/imports.xml" xxforms:username="admin"
				xxforms:password="admin" replace="instance" instance="imports"/>

			<xforms:submission id="save-imports" ref="instance('imports')" xxforms:username="admin"
				xxforms:password="admin" action="/exist/rest/db/numishare/imports.xml" method="put"
				replace="none">
				<xforms:send submission="submitCommit"  ev:event="xforms-submit-done"/>	
				<xforms:setvalue ref="instance('status')" ev:event="xforms-submit-done">CSV import and associated files removed.</xforms:setvalue>
				<xforms:message ev:event="xforms-submit-error" level="modal">Error saving imports
					record.</xforms:message>
			</xforms:submission>
			
			<!-- send optimize -->
			<xforms:submission id="optimize" action="{instance('site-info')/solr_url}update"
				ref="instance('optimizeIndex')" instance="optimizeIndex" replace="none"
				method="post">
				<xforms:message ev:event="xforms-submit-error" level="modal">Failed to optimize Solr
					index.</xforms:message>
			</xforms:submission>

			<xforms:action ev:event="xforms-model-construct-done">
				<xforms:send submission="load-imports"/>
				<xforms:send submission="load-site-info"/>
			</xforms:action>
		</xforms:model>
	</head>
	<body>
		<div class="form">
			<xforms:group ref="instance('status')/text()">
				<div class="success">
					<xforms:output ref="instance('status')"/>
				</div>
			</xforms:group>

			<xforms:group ref="instance('imports')">
				<xforms:group ref=".[count(import) &gt; 0]">
					<div class="section">
						<h3>Imports</h3>
						<table>
							<thead>
								<th>File Name</th>
								<th>Import Date</th>
								<th>Files</th>
								<th>Delete?</th>
							</thead>
							<tbody>
								<xforms:repeat nodeset="import">
									<xxforms:variable name="date" select="@date"/>
									<tr>
										<td>
											<xforms:output value="@filename"/>
										</td>
										<td>
											<xforms:output value="$date"/>
										</td>
										<td style="text-align:center">
											<xforms:output value="count(file)"/>
										</td>
										<td style="text-align:center">
											<xforms:trigger appearance="minimal">
												<xforms:action ev:event="DOMActivate">
												<xforms:setvalue ref="instance('timestamp')"
												value="$date"/>
												<xforms:dispatch target="remove-import-dialog"
												name="fr-show"/>
												</xforms:action>

												<xforms:label>
												<img src="/apps/numishare/images/remove.gif"/>
												</xforms:label>
											</xforms:trigger>
										</td>
									</tr>
								</xforms:repeat>
							</tbody>
						</table>
					</div>
				</xforms:group>
				<xforms:group ref=".[count(import) = 0]">
					<h3>Imports</h3>
					<p>There have been no imports from CSV yet.</p>
				</xforms:group>
			</xforms:group>

			<fr:alert-dialog id="remove-import-dialog">
				<fr:label>Confirm Delete</fr:label>
				<fr:message> Are you sure you wish to undo this import? </fr:message>
				<fr:negative-choice/>
				<fr:positive-choice>
					<xforms:action ev:event="DOMActivate">
						<xforms:action
							xxforms:iterate="instance('imports')/import[@date = instance('timestamp')]/file">
							<xxforms:variable name="id" select="@id"/>
							<xforms:setvalue ref="instance('current-id')" value="$id"/>
							<xforms:setvalue ref="instance('deleteId')/id" value="$id"/>
							<xforms:send submission="delete-submission"/>
						</xforms:action>
						<xforms:delete
							context="instance('imports')/import[@date = instance('timestamp')]"/>
						<xforms:send submission="save-imports"/>
					</xforms:action>
				</fr:positive-choice>
			</fr:alert-dialog>

			<!--<widget:xforms-instance-inspector id="orbeon-xforms-inspector"
				xmlns:widget="http://orbeon.org/oxf/xml/widget"/>-->
		</div>
	</body>
</html>
